<template>
  <el-dialog
    :title="editId ? '编辑类别' : '新增类别'"
    v-model="dialogVisible"
    width="40%"
    :before-close="handleClose"
  >
    <el-form
      :model="cateForm"
      :rules="rules"
      ref="cate-Form"
      label-width="100px"
    >
      <el-form-item label="上级分类" prop="pid">
        <el-select v-model="cateForm.pid">
          <el-option :value="0" label="一级分类"></el-option>
          <el-option
            v-for="cate in cates.cateList"
            :label="cate.catename"
            :value="cate.id"
            :key="cate.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" prop="catename">
        <el-input
          v-model="cateForm.catename"
          autocomplete="off"
          placeholder="请输入分类名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="封面图片" v-if="cateForm.pid !== 0">
        <el-upload
          action="#"
          list-type="picture-card"
          :auto-upload="false"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :limit="1"
          :on-exceed="limitTips"
          :on-change="changeFile"
          :file-list="fileList"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
        <el-dialog v-model="Visible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio v-model="cateForm.status" :label="1">启用</el-radio>
        <el-radio v-model="cateForm.status" :label="2">禁用</el-radio>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submitCates()"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
// 导入store
import useCateStore from "../../stores/cate";
// 声明
const cates = useCateStore();

// 定义控制弹窗显隐的变量
const dialogVisible = ref(false);

//关闭弹窗？
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭吗?", "关闭弹窗！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      done();
      //   重置表单
      resetForm();
    })
    .catch(() => {
      // catch error
    });
};

// 关闭弹窗
const closeDialog = () => {
  resetForm();
};

// 定义菜单表单
let cateForm = reactive({
  pid: 0,
  catename: "",
  img: "",
  status: 1,
});

// 表单规则
const rules = reactive({
  pid: { required: true, message: "选择上级分类", trigger: "blur" },
  catename: { required: true, message: "输入分类名称", trigger: "blur" },
  img: { required: true, message: "选择封面图片", trigger: "blur" },
  status: { required: true, message: "选择类别状态", trigger: "blur" },
});

// 定义编辑id
let editId = ref("");

// 清空表单，关闭弹窗
const resetForm = () => {
  // 重置编辑id
  editId = ref("");
  // 初始化表单
  cateForm = reactive({
    pid: 0,
    catename: "",
    img: "",
    status: 1,
  });
  //   重置文件列表
  fileList = reactive([]);

  // 关闭弹窗
  dialogVisible.value = false;
};

// 图片路径
const dialogImageUrl = ref("");
// 预览图窗口可视
const Visible = ref(false);
// 上传的文件列表
var fileList = reactive([]);
// 手动移除文件
const handleRemove = (file, fileList) => {
  console.log(file, fileList);
};
// 图片预览函数;
const handlePictureCardPreview = (file) => {
  dialogImageUrl.value = file.url;
  Visible.value = true;
};
// 下载图片
const handleDownload = (file) => {
  console.log(file);
};
// 文件超出限制数量的提示
const limitTips = () => {
  ElMessage({
    type: "warning",
    message: "你只能选择一张图片作为封面",
  });
};
const changeFile = (file) => {
  //   console.log("修改了文件", file);
  //   设置表单封面图
  cateForm.img = file.raw;
};
watch(
  () => cateForm.pid,
  (pid) => {
    if (pid === 0) {
      // 用户选择一级分类时，清空分类图片
      cateForm.img = "";
    }
  }
);

// 提交菜单
const submitCates = () => {
  //   console.log("cateForm", cateForm);
  //   console.log("editId", editId);
  if (editId.value) {
    cates.editCateActions({ ...cateForm, id: editId.value });
  } else {
    cates.addCateActions(cateForm);
  }
  resetForm();
};

// 接收父组件数据
const acceptValue = (value) => {
  console.log("value", value);
  editId.value = value.id;
  cateForm = reactive({ ...value });
  fileList.push({
    url: "http://localhost:3000" + value.img,
  });
};

// 将变量与函数暴露出来,此代码片段放到下面
defineExpose({
  dialogVisible,
  acceptValue,
});
</script>

<style lang="css" scoped>
:v-deep .el-dialog {
  width: 100% !important;
}
.el-select {
  width: 100%;
}
</style>
